<html>
<meta charset="utf-8">
<title>制版</title>
</head>

<body onload="getAllTemplelists()" style="text-align: center;">
    <script type="text/javascript" src='js/main.js'></script>
    <link rel="stylesheet" href="css/main.css">
    <div id="search">
        <span>关键词：</span>
        <input id="content" type="text" style="width:245px">
        <button type="button" onClick="search()">查询</button>
        <br />
        <br />
    </div>
    <select id="templates" size=8 style='width:99%'>
        <!--这里ajax动态拉取。-->
    </select>
    <br />
    <br />
    <div style="text-align: right"><button type="button" class="bulebutton" onClick="selectTemplate()">确定</button></div>
</body>

</html>

<script>
    console.log("这是套红头（制版）页面");
    var l_Doc = wps.WpsApplication().ActiveDocument;
    function selectTemplate()
    {
        var wpsApp = wps.WpsApplication();
        var obj = document.getElementById("templates");
        var index = obj.selectedIndex;
        if (index == -1)
        { //添加未选中数据时的异常处理
            alert("请先选择红头文件后再进行制版！");
            return;
        }
        var redId = obj.options[index].getAttribute("value");
        var activeDoc = wpsApp.ActiveDocument;
        var redHeadBase = GetDocParamsValue(l_Doc, "redHeadBase");
        //var path = redHeadBase + "&templateunid="+redId;
        var path = redHeadBase + redId;
        //alert(path);
        SetDocParamsValue(activeDoc, "insertFileUrl", path);
        InsertRedHeadDoc(activeDoc, insetRedHeadDocAfther);
    }

    function insetRedHeadDocAfther()
    {
        window.opener = null;
        window.open('', '_self', '');
        window.close();
    }

    function getAllTemplelists()
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function ()
        {
            //当接受到响应时回调该方法
            if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 0))
            {
                var text = xmlhttp.responseText; //使用接口返回内容，响应内容
                console.log("获取到的红头模板列表:" + text);
                if (text != null && text.length > 0)
                {
                    var resultJson = JSON.parse(text) //将json字符串转换成对象
                    for (var i = 0; i < resultJson.length; i++)
                    {
                        var element = resultJson[i]
                        var myOption = document.createElement("option"); //动态创建option标签
                        myOption.value = element.tempId; //红头文档id
                        myOption.text = element.tempName; //红头文档名称
                        templates.add(myOption);
                        console.log("element.tempName1:" + element.tempName);
                    }

                    if (resultJson.length == 1)
                    {
                        //自动制版
                        var redId = resultJson[0].tempId;
                        var wpsApp = wps.WpsApplication();
                        var activeDoc = wpsApp.ActiveDocument;
                        var redHeadBase = GetDocParamsValue(l_Doc, "redHeadBase");
                        //var path = redHeadBase + "&templateunid="+redId;
                        var path = redHeadBase + redId;
                        //alert(path);
                        SetDocParamsValue(activeDoc, "insertFileUrl", path);
                        InsertRedHeadDoc(activeDoc, insetRedHeadDocAfther);
                    }
                }
            }
        }
        var redHeadsPath = GetDocParamsValue(l_Doc, "redHeadsPath");
        xmlhttp.open("GET", redHeadsPath, true); //以POST方式请求该接口
        xmlhttp.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded;charset=UTF-16LE"); //添加Content-type
        xmlhttp.send(); //发送请求参数间用&分割
        if (!wps.PluginStorage.getItem("searchRedHeadPath"))
        {
            document.getElementById("search").style.display = "none";
        }
    }

    function search()
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function ()
        {
            //当接受到响应时回调该方法
            if (xmlhttp.readyState == 4 && (xmlhttp.status == 200 || xmlhttp.status == 0))
            {
                var text = xmlhttp.responseText; //使用接口返回内容，响应内容
                var resultJson = JSON.parse(text) //将json字符串转换成对象
                templates.options.length = 0;
                for (var i = 0; i < resultJson.length; i++)
                {
                    var element = resultJson[i]
                    var myOption = document.createElement("option"); //动态创建option标签
                    myOption.value = element.tempId; //红头文档id
                    myOption.text = element.tempName; //红头文档名称
                    templates.add(myOption);
                }
            }
        }

        var searchPath = wps.PluginStorage.getItem("searchRedHeadPath") || OA_DOOR.redHeadsPath

        var totalPath = searchPath + "?content=" + document.getElementById("content").value;

        xmlhttp.open("get", totalPath, true); //以POST方式请求该接口
        xmlhttp.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded;charset=UTF-16LE"); //添加Content-type
        xmlhttp.send(); //发送请求参数间用&分割
    }

</script>